package pt.mauromad.guias.dao.security;

import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

import pt.mauromad.guias.dao.UserDao;

@Entity
@Table(name = "role")
public class RoleDao {
	public static final RoleDao ROLE_USER = get("USER_ROLE");
	public static final RoleDao ROLE_ADMIN = get("ROLE_ADMIN");
	public static final RoleDao ROLE_ACCOUNT_USER = get("ROLE_ACCOUNT_USER");
	public static final RoleDao ROLE_ACCOUNT_MANAGER = get("ROLE_ACCOUNT_MANAGER");

	private static RoleDao get(String role) {
		RoleDao tmp = new RoleDao();
		tmp.setRole(role);
		return tmp;
	}

	private String role;
	private Set<UserDao> users;

	@Id
	public String getRole() {
		return role;
	}

	public void setRole(String role) {
		this.role = role;
	}

	@ManyToMany(fetch = FetchType.LAZY, mappedBy = "roles")
	public Set<UserDao> getUsers() {
		return users;
	}

	public void setUsers(Set<UserDao> users) {
		this.users = users;
	}

	@Override
	public int hashCode() {
		return role.hashCode();
	}

	@Override
	public boolean equals(Object obj) {
		return (obj instanceof RoleDao) && this.role.equals(((RoleDao) obj).role);
	}
}
